package org.mzyyn.qr.config;


import cn.hutool.core.util.StrUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description TODO
 * @Author lipeng
 * @Date 2023/1/9 11:15
 **/
@Configuration
public class RedissionConfig {

    @Value("${spring.data.redis.host}")
    private String host;

    @Value("${spring.data.redis.port}")
    private String port;

    @Value("${spring.data.redis.password}")
    private String password;

    @Value("${spring.data.redis.database}")
    private Integer database;

    /**
     * RedissonClient,单机模式
     *
     * @return RedissonClient
     */
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() {
        if(StrUtil.isEmpty(password)){
            password = null;
        }
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port)
                .setPassword(password).setDatabase(database)
                //Ping时间间隔（毫秒），0代表禁止
                .setPingConnectionInterval(0);
        return Redisson.create(config);
    }
}